package com.joysuch.wwyt.datadump.biz.processor;

import cn.hutool.core.lang.Assert;
import cn.zlg.common.datadump.annotation.DataDumpEntity;
import cn.zlg.common.datadump.processor.BaseImportEntityProcessor;
import com.alibaba.fastjson.JSONArray;
import com.google.common.base.Splitter;
import com.joysuch.wwyt.bp.repository.BPNFCLocationDao;
import com.joysuch.wwyt.common.constant.Constant;
import com.joysuch.wwyt.core.entity.BaseUser;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.datadump.biz.model.RiskPointDumpBean;
import com.joysuch.wwyt.risk.entity.RiskDistrict;
import com.joysuch.wwyt.risk.entity.RiskPoint;
import com.joysuch.wwyt.risk.entity.RiskPointType;
import com.joysuch.wwyt.risk.entity.dto.UserBean;
import com.joysuch.wwyt.risk.repository.RiskPointDao;
import com.joysuch.wwyt.risk.service.RiskPointService;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author ZhaoQiuWei
 * @Date 2021/12/16 19:44
 */
@Component
public class RiskPointImportProcessor extends BaseImportEntityProcessor<RiskPointDumpBean, RiskPoint> {

    @Autowired
    private BPNFCLocationDao nfcDao;
    @Autowired
    private RiskPointService riskPointService;
    @Autowired
    private RiskPointDao riskPointDao;

    @Autowired
    private BaseUserDao userDao;

    @Override
    public RiskPoint processBeforePersist(RiskPointDumpBean origin, RiskPoint entity, DataDumpEntity annotation, boolean override) {
        Integer signInMethod = entity.getSignInMethod();
        Assert.isTrue(signInMethod != null, "风险点排查签到方式不正确,请填写:不签到 NFC签到 定位签到");
        String nfc = entity.getNfc();
        String type = entity.getType();
        String subTypeName = entity.getSubType();
        String identifyUserCodes = entity.getIdentifyUserCodes();
        if (StringUtils.isNotBlank(identifyUserCodes)){
            List<String> identifyUserCodeList = Splitter.on(Constant.SPLIT_COMMA).trimResults().splitToList(identifyUserCodes.trim());
            if (!CollectionUtils.isEmpty(identifyUserCodeList)){
                List<BaseUser> baseUsers = userDao.findByCodes(identifyUserCodeList);
                if (!CollectionUtils.isEmpty(baseUsers)){
                    List<UserBean> userBeans = baseUsers.stream().map(baseUser -> {
                        UserBean bean = new UserBean();
                        bean.setId(baseUser.getId());
                        bean.setName(baseUser.getRealName());
                        bean.setCode(baseUser.getCode());
                        return bean;
                    }).collect(Collectors.toList());
                    entity.setIdentifyUserStr(JSONArray.toJSONString(userBeans));
                }
            }
        }
        if (StringUtils.isNotBlank(subTypeName) && StringUtils.isNotBlank(type)) {
            RiskPointType subType = riskPointService.checkTypeAndSubType(type, subTypeName);
            Assert.isTrue(subType != null, "风险点子类型匹配异常");
            if (subType != null) {
                entity.setSubType(subType.getCode());
            }
        }
        //签到方式(0：人员定位，1：刷卡签到，2：不签到)
        if (signInMethod == 0) {
            //默认校验楼层
            entity.setDistinguishFloor(1);
        } else if (signInMethod == 1) {
            Assert.isTrue(StringUtils.isNotBlank(nfc), "签到方式为扫码签到,NFC卡号不能为空");
            Assert.isTrue(!CollectionUtils.isEmpty(nfcDao.findByCode(nfc)), "NFC识别码不存在");
        }
        RiskDistrict riskDistrict = entity.getRiskDistrict();
        Assert.isTrue(riskDistrict != null, "风险区域不存在");
        RiskPoint point = null;
        if (entity.getId() == null) {
            point = riskPointDao.getOnebyNameAndDistrictId(entity.getName(), entity.getRiskDistrict().getId());
        } else {
            point = riskPointDao.getOnebyNameAndDistrictIdAndId(entity.getName(), entity.getRiskDistrict().getId(), entity.getId());
        }
        Assert.isTrue(point == null, "风险单元名称[" + entity.getName() + "]在该区域下已存在");
        return entity;
    }
}
